<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">

<head th:include="heard :: head"></head>

<body>
<nav th:replace="heard :: nav"></nav>
<div class="container-fluid" style="font-size: 14px;">
    <form id="formPV">
        <input id="pid" name="parentId" style="display:none" th:value="${pid}">
        <input name="id" type="text" style="display:none" th:value="${pv!=null?pv.get('id'):''}">
        <div class="form-group row ">
            <label for="inputCode" class="col-sm-2 col-form-label">编码</label>
            <div class="col-sm-8">
                <input type="text" class="form-control form-control-sm"
                       id="inputCode" name="code" th:value="${pv!=null?pv.get('code'):''}"
                       placeholder=""/></div>
        </div>
        <div class="form-group row">
            <label for="inputName" class="col-sm-2 col-form-label">名称</label>
            <div class="col-sm-8">
                <input type="text" class="form-control form-control-sm"
                       id="inputName" name="name" th:value="${pv!=null?pv.get('name'):''}"
                       placeholder=""/></div>
        </div>
        <!-- 编辑 -->
        <div th:if="${pv}!=null" th:each="item,status: ${pv}">

            <div class="form-group row">
                <label th:unless="${status.current.key eq 'id'} or ${status.current.key eq 'code'}  or ${status.current.key eq 'name'}" th:for="'inpunt'+${status.current.key}"
                       th:text="${status.current.key}"
                       class="col-sm-2 col-form-label"></label>

                <div class="col-sm-8">
                    <input th:if="${status.current.key eq 'id'}"
                           type="text" class="form-control form-control-sm"
                           style="display:none" th:id="'inpunt'+${status.current.key}"
                           th:name="${status.current.key}"
                           th:value="${status.current.value}">


                    <input th:unless="${status.current.key eq 'id'} or ${status.current.key eq 'code'}  or ${status.current.key eq 'name'}"
                           type="text" class="form-control form-control-sm"
                           th:id="'inpunt'+${status.current.key}" th:name="${status.current.key}"
                           th:value="${status.current.value}">
                </div>
            </div>
        </div>
        <!-- 新增 -->
        <div th:if="${pd}!=null" th:each="item,status: ${pd}">
            <div class="form-group row">
                <label th:for="'inpunt'+${item.name}" th:text="${item.name}" class="col-sm-2 col-form-label"></label>
                <div class="col-sm-8">

                    <input type="text" class="form-control form-control-sm"
                           th:id="'inpunt'+${item.name}" th:name="${item.name}">
                </div>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-sm-10">
                <button type="button" onclick="savePV()" class="btn btn-primary">保存</button>
            </div>
        </div>
    </form>
</div>

</div>

</body>
<script>
    function savePV() {
        var fields = $('#formPV').serializeArray();
        var obj = {}; //声明一个对象
        $.each(fields, function (index, field) {
            obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
        })
        $.ajax({
            type: "post",
            url: "/parameter/value",
            async: true,
            contentType: 'application/json',
            dataType: 'JSON',
            data: JSON.stringify(obj),//将对象转为json字符串
            success: function (obj) {
                let pid = $("#pid").val();
                window.location.href = "/home/p/" + pid + "/v";
                //window.history.back()
            },
            error: function (obj) {
                alert(obj.msg);
            }
        });
    }
</script>

</html>
